﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Base;
using svnAccessFile;

namespace ApacheConfFile
{
    public enum lAuthType { Basic };
    public enum lEncryption { md5 };
    public class Location : baseItem<Location>
    {
        
        public string topComment { get; set; }
        public string urlName 
        {
            get
            {
                return Name;
            }
            set
            {
                Name = value;
            }
        }
        public bool SVNListParentPath { get; set; }
        public string SVNParentPath { get; set; }
        public lAuthType AuthType { get; set; }
        public string AuthName { get; set; }
        public bool useAuthUserFile { get; set; }
        public string AuthUserFile { get; set; }

        public string AuthMySQLHost { get; set; }
        public string AuthMySQLUser { get; set; }
        public string AuthMySQLPassword { get; set; }
        public string AuthMySQLDB { get; set; }
        public string AuthMySQLUserTable { get; set; }
        public string AuthMySQLNameField { get; set; }
        public string AuthMySQLPasswordField { get; set; }
        public lEncryption AuthMySQLPwEncryption { get; set; }

        public string AuthzSVNAccessFile { get; set; }
        public bool RequireValidUser { get; set; }

        public string autoVersionComment { get; set; }
        public bool SVNAutoversioning { get; set; }
        public bool ModMimeUsePathInfo { get; set; }

        SvnAccess svnAccess { get; set; }


        public Location()
        {
            svnAccess = null;
        }
            
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            if(topComment.Length >0)
            {
                sb.AppendFormat("#{0}\r\n", topComment);
            }
            sb.AppendFormat("<Location /{0}>\r\n", urlName);
            sb.AppendLine("DAV svn");
            if(SVNListParentPath)
            {
                sb.AppendLine("SVNListParentPath on");
                sb.AppendFormat("SVNParentPath {0}\r\n", SVNParentPath);
            }
            sb.AppendFormat("AuthType {0}\r\n", AuthType.ToString());
            sb.AppendFormat("AuthName \"{0}\"\r\n", AuthName);
            sb.AppendFormat("{0}AuthUserFile {1}\r\n", useAuthUserFile?"":"#",AuthUserFile);
            sb.AppendLine("");

            sb.AppendFormat("AuthMySQLHost {0}\r\n", AuthMySQLHost);
            sb.AppendFormat("AuthMySQLUser {0}\r\n", AuthMySQLUser);
            sb.AppendFormat("AuthMySQLPassword {0}\r\n", AuthMySQLPassword);
            sb.AppendFormat("AuthMySQLDB {0}\r\n", AuthMySQLDB);
            sb.AppendFormat("AuthMySQLUserTable {0}\r\n", AuthMySQLUserTable);
            sb.AppendFormat("AuthMySQLNameField {0}\r\n", AuthMySQLNameField);
            sb.AppendFormat("AuthMySQLPasswordField {0}\r\n", AuthMySQLPasswordField);
            sb.AppendFormat("AuthMySQLPwEncryption {0}\r\n", AuthMySQLPwEncryption);
            sb.AppendLine("");
            

            
            sb.AppendFormat("AuthzSVNAccessFile {0}\r\n", AuthzSVNAccessFile);
            if(RequireValidUser)
            {
                sb.AppendLine("Require valid-user");
            }
            if(autoVersionComment.Length>0)
            {
                if(autoVersionComment.Length>0)
                {
                    sb.AppendFormat("#{0}\r\n", autoVersionComment);
                }
                sb.AppendLine("SVNAutoversioning on");
                if(autoVersionComment.Length>0)
                {
                    sb.AppendLine("ModMimeUsePathInfo on");
                }
            }

            sb.AppendLine("</Location>");

            return sb.ToString();
        }

    }
    public class Locations : baseList<Location>
    {
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            foreach (Location loc in this)
            {
                sb.AppendLine(loc.ToString());
                sb.AppendLine("");
            }

            return sb.ToString();
        }
    }
}
